﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IPC.Communication.Framework.Core;
using IPC.Communication.Framework.Core.Address;
using IPC.Communication.Framework.Core.Net;
using IPC.Communication.Framework.Profinet.Melsec.Helper;
using IPC.Communication.Framework.Reflection;

namespace IPC.Communication.Framework.Profinet.Melsec
{
    public class MelsecMcAsciiUdp : NetworkUdpDeviceBase, IReadWriteMc, IReadWriteDevice, IReadWriteNet
    {
        private McType _mcType = McType.MCAscii;
        private byte _networkNumber = 0;
        private byte _networkStationNumber = 0;

        public McType McType
        {
            get { return _mcType; }
            set { _mcType = value; }
        }

        public byte NetworkNumber
        {
            get { return _networkNumber; }
            set { _networkNumber = value; }
        }


        public byte NetworkStationNumber
        {
            get { return _networkStationNumber; }
            set { _networkStationNumber = value; }
        }


        public MelsecMcAsciiUdp()
        {
            base.WordLength = 1;
            LogMsgFormatBinary = false;
            base.ByteTransform = new RegularByteTransform();
        }

        public MelsecMcAsciiUdp(string ipAddress, int port)
            : this()
        {
            IpAddress = ipAddress;
            Port = port;
        }

        public virtual OperateResult<McAddressData> McAnalysisAddress(string address, ushort length)
        {
            return McAddressData.ParseMelsecFrom(address, length);
        }

        protected override byte[] PackCommandWithHeader(byte[] command)
        {
            return McAsciiHelper.PackMcCommand(command, NetworkNumber, NetworkStationNumber);
        }

        protected override OperateResult<byte[]> UnpackResponseContent(byte[] send, byte[] response)
        {
            OperateResult operateResult = McAsciiHelper.CheckResponseContent(response);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[]>(operateResult);
            }
            return OperateResult.CreateSuccessResult(response.RemoveBegin(22));
        }

        public byte[] ExtractActualData(byte[] response, bool isBit)
        {
            return McAsciiHelper.ExtractActualDataHelper(response, isBit);
        }

        [HslMqttApi("ReadByteArray", "")]
        public override OperateResult<byte[]> Read(string address, ushort length)
        {
            return McHelper.Read(this, address, length);
        }

        [HslMqttApi("WriteByteArray", "")]
        public override OperateResult Write(string address, byte[] value)
        {
            return McHelper.Write(this, address, value);
        }

        [HslMqttApi("随机读取PLC的数据信息，可以跨地址，跨类型组合，但是每个地址只能读取一个word，也就是2个字节的内容。收到结果后，需要自行解析数据")]
        public OperateResult<byte[]> ReadRandom(string[] address)
        {
            return McHelper.ReadRandom(this, address);
        }

        [HslMqttApi(ApiTopic = "ReadRandoms", Description = "随机读取PLC的数据信息，可以跨地址，跨类型组合，每个地址是任意的长度。收到结果后，需要自行解析数据，目前只支持字地址，比如D区，W区，R区，不支持X，Y，M，B，L等等")]
        public OperateResult<byte[]> ReadRandom(string[] address, ushort[] length)
        {
            return McHelper.ReadRandom(this, address, length);
        }

        public OperateResult<short[]> ReadRandomInt16(string[] address)
        {
            return McHelper.ReadRandomInt16(this, address);
        }

        public OperateResult<ushort[]> ReadRandomUInt16(string[] address)
        {
            return McHelper.ReadRandomUInt16(this, address);
        }

        [HslMqttApi("ReadBoolArray", "")]
        public override OperateResult<bool[]> ReadBool(string address, ushort length)
        {
            return McHelper.ReadBool(this, address, length);
        }

        [HslMqttApi("WriteBoolArray", "")]
        public override OperateResult Write(string address, bool[] values)
        {
            return McHelper.Write(this, address, values);
        }

        [HslMqttApi(ApiTopic = "ReadExtend", Description = "读取扩展的数据信息，需要在原有的地址，长度信息之外，输入扩展值信息")]
        public OperateResult<byte[]> ReadExtend(ushort extend, string address, ushort length)
        {
            return McHelper.ReadExtend(this, extend, address, length);
        }

        [HslMqttApi(ApiTopic = "ReadMemory", Description = "读取缓冲寄存器的数据信息，地址直接为偏移地址")]
        public OperateResult<byte[]> ReadMemory(string address, ushort length)
        {
            return McHelper.ReadMemory(this, address, length);
        }

        [HslMqttApi(ApiTopic = "ReadSmartModule", Description = "读取智能模块的数据信息，需要指定模块地址，偏移地址，读取的字节长度")]
        public OperateResult<byte[]> ReadSmartModule(ushort module, string address, ushort length)
        {
            return McHelper.ReadSmartModule(this, module, address, length);
        }

        [HslMqttApi(ApiTopic = "RemoteRun", Description = "远程Run操作")]
        public OperateResult RemoteRun()
        {
            return McHelper.RemoteRun(this);
        }

        [HslMqttApi(ApiTopic = "RemoteStop", Description = "远程Stop操作")]
        public OperateResult RemoteStop()
        {
            return McHelper.RemoteStop(this);
        }

        [HslMqttApi(ApiTopic = "RemoteReset", Description = "LED 熄灭 出错代码初始化")]
        public OperateResult RemoteReset()
        {
            return McHelper.RemoteReset(this);
        }

        [HslMqttApi(ApiTopic = "ReadPlcType", Description = "读取PLC的型号信息，例如 Q02HCPU")]
        public OperateResult<string> ReadPlcType()
        {
            return McHelper.ReadPlcType(this);
        }

        [HslMqttApi(ApiTopic = "ErrorStateReset", Description = "LED 熄灭 出错代码初始化")]
        public OperateResult ErrorStateReset()
        {
            return McHelper.ErrorStateReset(this);
        }

        public override string ToString()
        {
            return String.Format("MelsecMcAsciiUdp[{0}:{1}]", IpAddress, Port);
        }
    }
}
